{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 腐蚀操作",
   "id": "c5875b48e8d4d240"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-18T13:47:51.512193Z",
     "start_time": "2025-09-18T13:47:45.145698Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sympy.vector import gradient\n",
    "\n",
    "img = cv2.imread(r'../data/dige.png')\n",
    "cv2.imshow('img', img)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "id": "18a3ad626af12fcd",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-18T13:47:59.043216Z",
     "start_time": "2025-09-18T13:47:53.874928Z"
    }
   },
   "cell_type": "code",
   "source": [
    "kernel = np.ones((5,5), np.uint8)\n",
    "erosion = cv2.erode(img, kernel, iterations=1)\n",
    "cv2.imshow('erosion', erosion)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()\n",
    "\n",
    "res = np.hstack((img, erosion))\n",
    "cv2.imshow('res', res)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "id": "499689b032386d32",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T07:53:24.611726Z",
     "start_time": "2025-09-15T07:53:22.006036Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pie = cv2.imread(r'../data/pie.png')\n",
    "cv2.imshow('pie', pie)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "id": "575cd2bfb3da0c4a",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T07:55:14.067996Z",
     "start_time": "2025-09-15T07:55:11.460710Z"
    }
   },
   "cell_type": "code",
   "source": [
    "kernel = np.ones((30, 30), np.uint8)\n",
    "erosion_1 = cv2.erode(pie, kernel, iterations=1)\n",
    "erosion_2 = cv2.erode(pie, kernel, iterations=2)\n",
    "erosion_3 = cv2.erode(pie, kernel, iterations=3)\n",
    "res = np.hstack((erosion_1, erosion_2, erosion_3))\n",
    "cv2.imshow('res', res)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "id": "80cd61ec8fc8c6ff",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 膨胀操作",
   "id": "e7e5b6e54c458be9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-18T13:48:11.430468Z",
     "start_time": "2025-09-18T13:48:06.792258Z"
    }
   },
   "cell_type": "code",
   "source": [
    "img = cv2.imread(r'../data/dige.png')\n",
    "kernel = np.ones((3,3), np.uint8)\n",
    "# 先进行腐蚀，再进行膨胀\n",
    "dige_erosion = cv2.erode(img, kernel, iterations=1)\n",
    "dige_dilation = cv2.dilate(dige_erosion, kernel, iterations=1)\n",
    "cv2.imshow('dige_dilation', dige_dilation)\n",
    "# cv2.imshow('dige_erosion', dige_erosion)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "id": "e5d62107c9ab015f",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 开运算与闭运算",
   "id": "8393e211d2f36149"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-18T13:48:33.641104Z",
     "start_time": "2025-09-18T13:48:28.047412Z"
    }
   },
   "cell_type": "code",
   "source": [
    "img = cv2.imread(r'../data/dige.png')\n",
    "kernel = np.ones((3,3), np.uint8)\n",
    "dige_open = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)\n",
    "dige_close = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)\n",
    "res = np.hstack((dige_open, dige_close))\n",
    "cv2.imshow('res', res)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "id": "6b2d619786759100",
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T08:04:02.222753Z",
     "start_time": "2025-09-15T08:03:53.746904Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pie = cv2.imread(r'../data/pie.png')\n",
    "kernel = np.ones((30, 30), np.uint8)\n",
    "pie_open = cv2.morphologyEx(pie, cv2.MORPH_OPEN, kernel)\n",
    "pie_close = cv2.morphologyEx(pie, cv2.MORPH_CLOSE, kernel)\n",
    "res = np.hstack((pie_open, pie_close))\n",
    "cv2.imshow('res', res)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "id": "a8fec7a29cef59a5",
   "outputs": [],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 梯度运算",
   "id": "6ab0fcac6800d8e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T08:08:35.321125Z",
     "start_time": "2025-09-15T08:08:33.393412Z"
    }
   },
   "cell_type": "code",
   "source": [
    "pie = cv2.imread(r'../data/pie.png')\n",
    "kernel = np.ones((7, 7), np.uint8)\n",
    "pie_erode = cv2.erode(pie, kernel, iterations=5)\n",
    "pie_dilate = cv2.dilate(pie, kernel, iterations=5)\n",
    "\n",
    "res = np.hstack((pie_dilate, pie_erode))\n",
    "cv2.imshow('res', res)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "id": "60a92f6d65fa99ff",
   "outputs": [],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-15T08:09:15.864497Z",
     "start_time": "2025-09-15T08:09:12.133567Z"
    }
   },
   "cell_type": "code",
   "source": [
    "gradient = cv2.morphologyEx(pie, cv2.MORPH_GRADIENT, kernel)\n",
    "\n",
    "cv2.imshow('gradient', gradient)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "id": "a956093e219e1e06",
   "outputs": [],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 礼帽与黑帽\n",
    "- 礼帽： 原始输入 - 开运算结果\n",
    "- 黑帽： 闭运算结果 - 原始输入"
   ],
   "id": "c3074777138cb37b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-18T13:52:12.674915Z",
     "start_time": "2025-09-18T13:52:09.625482Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 礼帽\n",
    "img = cv2.imread(r'../data/dige.png')\n",
    "tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)\n",
    "cv2.imshow('tophat', tophat)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "id": "74f0f8d51b60204e",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-18T13:52:36.660781Z",
     "start_time": "2025-09-18T13:52:33.899864Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 黑帽\n",
    "img = cv2.imread(r'../data/dige.png')\n",
    "blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel)\n",
    "cv2.imshow('blackhat', blackhat)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ],
   "id": "3ee0ad172c940855",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "df5ed2779dca4e2f"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
